package com.bootdo.per.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.common.controller.BaseController;
import com.bootdo.common.utils.DateUtils;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.per.domain.TeamDO;
import com.bootdo.per.service.TeamService;
import com.bootdo.system.service.DeptService;
import com.bootdo.system.service.UserService;

@Controller	
@RequestMapping("/per/teamReport")
public class TeamReportController extends BaseController{
	@Autowired
	private TeamService teamService;
	@Autowired
	private DeptService deptService;
	@Autowired
	private UserService userService;
	
	@GetMapping()
	@RequiresPermissions("per:teamReport:teamReport")
	String teamReport(Model model){
		Map<String, Object> map = new HashMap<>();
		model.addAttribute("deptList", deptService.list(map));
		model.addAttribute("sysUserList",userService.list(map));
	    return "per/report/team_report";
	}
	
	@ResponseBody
	@GetMapping("/teamList")
	@RequiresPermissions("per:teamReport:teamReport")
	public PageUtils teamList(@RequestParam Map<String, Object> params){
		PageUtils pageUtils;
		Integer year = DateUtils.getYear();//年份
		Long deptId = (long) 0;//部门ID
		Long scoreUserId = (long) 0;//评分人ID
		
		String dpId = (String) params.get("deptId");
		if(dpId !=null && !"".equals(dpId)){
			deptId = Long.parseLong(dpId);
		}
		
		String suId = (String) params.get("scoreUserId");
		if(suId !=null && !"".equals(suId)){
			scoreUserId = Long.parseLong(suId);
		}
		
		if(params.get("year") != null && !"".equals(params.get("year"))){
			year = Integer.parseInt(params.get("year").toString());
		}
		
		//根据部门得到该部门所有团队的团队名
		List<String> deptList = teamService.getTeamByDeptAndSys(params);
		if(deptId != 0 || scoreUserId != 0){//根据部门得到该部门所有团队12个月的分数
			List<Map<String,Object>> reportList = getTeamReport(deptList);
			pageUtils = new PageUtils(reportList, reportList.size());
		}else{
			List<String> yearList = teamService.getTeamByYear(year);
			List<Map<String,Object>> reportList = getTeamReport(yearList);
			pageUtils = new PageUtils(reportList, reportList.size());
		}
		return pageUtils;
	}
	
	/**
	 * 得到每个团队12个月的分数
	 * @param list
	 * @return
	 */
	public List<Map<String,Object>> getTeamReport(List<String> list){
		List<Map<String,Object>> reportList = new ArrayList<Map<String,Object>>(); 
		//得到每个人12个月的分数
		for (int i = 0;i <= list.size()-1;i++) {
			String userName = list.get(i);
			List<TeamDO> listp = teamService.listAllScore(userName);
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("name", listp.get(0).getTeamName());
			//存放每个人12个月的分数
			for (TeamDO teamDO : listp) {
				if(teamDO.getMonth()==1){
					map.put("Jan", teamDO.getScore());
				}else if(teamDO.getMonth()==2){
					map.put("Feb", teamDO.getScore());
				}else if(teamDO.getMonth()==3){
					map.put("Mar", teamDO.getScore());
				}else if(teamDO.getMonth()==4){
					map.put("Apr", teamDO.getScore());
				}else if(teamDO.getMonth()==5){
					map.put("May", teamDO.getScore());
				}else if(teamDO.getMonth()==6){
					map.put("Jun", teamDO.getScore());
				}else if(teamDO.getMonth()==7){
					map.put("Jul", teamDO.getScore());
				}else if(teamDO.getMonth()==8){
					map.put("Aug", teamDO.getScore());
				}else if(teamDO.getMonth()==9){
					map.put("Sept", teamDO.getScore());
				}else if(teamDO.getMonth()==10){
					map.put("Oct", teamDO.getScore());
				}else if(teamDO.getMonth()==11){
					map.put("Nov", teamDO.getScore());
				}else if(teamDO.getMonth()==12){
					map.put("Dec", teamDO.getScore());
				}
			}
			reportList.add(map);
		}
		return reportList;
	}
}
